package nfa;

import java.util.HashSet;
import java.util.Set;

/**
 * @author haoly
 * @title NFA
 * @date 2025/4/16 14:42
 * @description NFA
 */
public class NFA<T> {
    private Set<T> currentStates;

    private final Set<T> acceptStates;

    private final NFARulebook<T> rulebook;

    public NFA(Set<T> currentStates, Set<T> acceptStates, NFARulebook<T> rulebook) {
        this.currentStates = currentStates;
        this.acceptStates = acceptStates;
        this.rulebook = rulebook;
    }

    public Boolean accepting(){
        Set<T> intersection = new HashSet<>(getCurrentStates());
        intersection.retainAll(this.acceptStates);
        return intersection.size() > 0;
    }

    public void readCharacter(Character character){
        Set<T> result = this.rulebook.nextStates(getCurrentStates(), character);
        this.currentStates = result;
    }

    public void readString(String s){
        for (int i = 0; i < s.length(); i++) {
            Character character = s.charAt(i);
            readCharacter(character);
        }
    }

    public Set<T> getCurrentStates() {
        return this.rulebook.followFreeMoves(this.currentStates);
    }

    public Set<T> getAcceptStates() {
        return this.acceptStates;
    }

    public NFARulebook<T> getRulebook() {
        return this.rulebook;
    }
}
